package com.doctorliang.cheng.shopping.app;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.doctorliang.cheng.shopping.R;
import com.doctorliang.cheng.shopping.base.BaseFragment;
import com.doctorliang.cheng.shopping.community.fragment.CommunityFragment;
import com.doctorliang.cheng.shopping.home.fragment.HomeFragment;
import com.doctorliang.cheng.shopping.shoppintcar.fragment.ShoopingCarFragment;
import com.doctorliang.cheng.shopping.type.fragment.TypeFragment;
import com.doctorliang.cheng.shopping.user.fragment.UserFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends FragmentActivity {

    @Bind(R.id.frameLayout)
    FrameLayout frameLayout;
    @Bind(R.id.rb_home)
    RadioButton rbHome;
    @Bind(R.id.rb_type)
    RadioButton rbType;
    @Bind(R.id.rb_community)
    RadioButton rbCommunity;
    @Bind(R.id.rb_cart)
    RadioButton rbCart;
    @Bind(R.id.rb_user)
    RadioButton rbUser;
    @Bind(R.id.rg_main)
    RadioGroup rgMain;

    /**
     * 记录各个fragmrnt的position
     */
    private int position;
    /**
     * 存各个fragment的集合
     */
    public List<BaseFragment>fragments;
        /**
         * 缓存的或者是上一个的fragment
         */
        private BaseFragment mContext;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragment();
        initListener();
    }

    /**
     * 初始化radiogroup的监听事件
     */
    private void initListener() {
        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.rb_home:
                        position = 0;
                        break;
                    case R.id.rb_type:
                        position = 1;

                        break;
                    case R.id.rb_community:
                        position = 2;

                        break;
                    case R.id.rb_cart:
                        position = 3;

                        break;
                    case R.id.rb_user:
                        position = 4;

                        break;
                    default:
                        break;
                }
                //根据不同的position往集合中得到不同的fragment
                BaseFragment baseFragment = getFragment(position);
                //切换不同的fragment
                Switch(mContext,baseFragment);
            }
        });
        //第一次默认显示主页
        rgMain.check(R.id.rb_home);
    }

    /**
     * 切换Fragment
     * @param tempFragment 当前一个fragment
     * @param baseFragment 即将切换的下一个fragment
     */
    private void Switch(Fragment tempFragment, BaseFragment baseFragment) {
        if (mContext!=baseFragment) {//两个fragment不等才切换
            //把baseFragment变成前一个
            mContext = baseFragment;
            if (baseFragment!=null) {//要切换的那一个部位空才进来
                //隐藏当前，显示下一个
                transaction = getSupportFragmentManager().beginTransaction();
                //判断即将切换的fragment是否已经添加过,没有就add，否则就show
                if (!baseFragment.isAdded()) {
                    //先隐藏当前
                    if (tempFragment != null) {
                        transaction.hide(tempFragment);
                    }
                    //add
                    transaction.add(R.id.frameLayout,baseFragment);//不应该用replace这里 应为当再次切换回来当前fragment是会重新加载 没记录 效果很不友好
                } else {
                    //先隐藏当前
                    if (tempFragment != null) {
                        transaction.hide(tempFragment);
                    }
                    //show
                    transaction.show(baseFragment);
                }
                transaction.commit();
            }
        }
    }

    /**
     * 根据位置取得fragment
     * @param position
     * @return
     */
    public BaseFragment getFragment(int position) {
        if (fragments!=null&&fragments.size()>0) {
            BaseFragment baseFragment = fragments.get(position);
            return baseFragment;
        }
        return null;
    }

    /**
     * 初始化各个fragment
     */
    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new TypeFragment());
        fragments.add(new CommunityFragment());
        fragments.add(new ShoopingCarFragment());
        fragments.add(new UserFragment());
    }
}
